<?php

defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package		Adminpro
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
ob_start();
class Watch extends MY_Controller {

	public function __construct(){
		parent:: __construct();
		$this->load->model('film_model');
		$this->load->model('film_info_model');
		$this->load->model('episode_model');
		$this->load->model('errorep_model');
		$this->load->model('category_model');
		$this->load->model('page_model');
		$this->load->model('config_model');
		$this->load->model('muser');
	}

	public function index(){
		$data                      = array();
		$rewrite_id 	           = RemoveHack($this->uri->segment(2));
		$ep_id					   = explode('-', $rewrite_id);
		$ep_id                     = end($ep_id);
		$param_get_film			   = array('epid','idphim','url','epname');
		$data['result_get_ep']     = $this->episode_model->get_info($ep_id,$param_get_film);
		$param_ep		   		   = array();
		$param_ep['select']		   = array('epid','epname','url');
		$param_ep['where']		   = array('idphim' => $data['result_get_ep']['idphim'],'server' => 2);
		$param_ep['order']         = array('epid','DESC');
		$data['result_ep']         = $this->episode_model->get_list($param_ep);
		$param                     = array('keywords','description','name','viewed','rewrite','search','idphim','thumb');
		$data['result']            = $this->film_model->get_info($data['result_get_ep']['idphim'],$param);
		$data['curent']            = base_url("info").'/'.$data['result']['rewrite'].'-f'.$data['result']['idphim'].'.html';  	
		$total 					   = (int)$data['result']['viewed'] + 1;
		$id                        = $data['result_get_ep']['idphim'];
		if(!empty($data['result_get_ep']['idphim'])){
			$this->film_model->save($data['result_get_ep']['idphim'],array('viewed' => $total));
		}
		$data['config']            = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']             = $data['result']['name'].' Episode '.$data['result_get_ep']['epname'];
		$data['title_url']         = base_url().$data['result']['name'].' Episode '.$data['result_get_ep']['epname'];
		$data['title_img']         = base_url().$data['result']['name'].' Episode '.$data['result']['thumb'];
		$data['keywords']          = $data['result']['keywords'];
		$data['description']       = $data['result']['name'].' Episode '.$data['result_get_ep']['epname'].' '.$data['result']['description'];
		$data['phone']             = $data['config']['cf_phone'];
		$data['email']             = $data['config']['cf_email'];
		$data['result_cate']       = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']              = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		$data['ongoing']           = $this->cache_site->get_info_cache($key = 'ongoing',$time = 1800,$this->film_model->film_new(30));
		$data['active']			   = $ep_id; 
		$data['sidebar']           = 'side_ongoing';
		$data['template']          = 'film/watch';
		$controls 				   = $this->input->post('controls');
		if(isset($controls)){
			$var      = array();
			$user_id  = $this->session->userdata('session_user_id');
			$datafilm = $this->muser->get_info($user_id,array('boxfilm','filmlike'));
			$film_id  = $data['result']['idphim'];
			if($controls == 'add-film'){
				if(!empty($datafilm['boxfilm'])){
					$arr_like = explode(',',$datafilm['boxfilm']);
					foreach ($arr_like as $key => $value) {
						if($value == $film_id){
							$var['error']   = 1;
							$var['content'] = 'you have add this movie..!';
						}else{
							$var['error']   = 0;
							$var['content'] = 'add a movie success';
							$arr_like[]     = $film_id;
						}					
					}
				}else{
					$arr_like = array();
					$arr_like[] =  $film_id;
				}
				$arr_data = array_unique($arr_like);
				$str_like = implode(',',$arr_data);
				$this->muser->save($user_id,array('boxfilm' => $str_like));
				unset($controls);
				exit(json_encode($var));
			}else if($controls == 'like-film'){
				if(!empty($datafilm['filmlike'])){
					$arr_like = explode(',',$datafilm['filmlike']);
					foreach ($arr_like as $key => $value) {
						if($value == $film_id){
							$var['error']   = 1;
							$var['content'] = 'you have like this movie..!';
						}else{
							$var['error']   = 0;
							$var['content'] = 'like a movie success';
							$arr_like[]     = $film_id;
						}					
					}
				}else{
					$arr_like = array();
					$arr_like[] =  $film_id;
				}
				$arr_data = array_unique($arr_like);
				$str_like = implode(',',$arr_data);
				$this->muser->save($user_id,array('filmlike' => $str_like));
				unset($controls);
				exit(json_encode($var));
			}else if($controls == 'error-film'){
				$param = array();
				$param['where'] = array('idep'=> $ep_id );
				$num = $this->errorep_model->get_total($param );
				if($num == 0){
					$db = array(
						'idep' => $ep_id,
						'idphim' => $film_id
					);
					$this->errorep_model->save($id=false,$db);
					$var['error']   = 0;
					$var['content'] = 'error message successfully episode';
				}else{
					$var['error']   = 1;
					$var['content'] = 'episode was the error';
				}
				unset($controls);
				exit(json_encode($var));
			}
		}
		$this->load->view("layout",$data);
	}

}
ob_flush();
/* End of file index.php */
/* Location: ./application/modules/home/controllers/index.php */